from BeautifulSoup import BeautifulSoup
from BeautifulSoup import NavigableString
import re
def get_text(element):
    names=element.findAll(text=True)
    text=''
    for x in names:
        text+=x            
    return text

def replace_with(text,old,new):
    try:
#        pattern="""'("""+old+""")'"""
#        p = re.search('(&lt;)',text).groups()
#        re.compile
#        print p
#        newstring=''
        return re.sub(old, new, text)
#        re.match
#        p.sub(new,text)
    except:
        return text     
def normalize_content(text):
    text=replace_with(text,'&lt;','<')
    text=replace_with(text,'&gt;','>')
    return text
def is_NavigableString(str):
    x=NavigableString('')
    if type(x)==type(str):
        return True
    return False         
                 
class element_a(object):
    def __init__(self,a):
        for id,name in a.attrs:
            if id=='href':
                self.href=name
#                print name
                self.title=get_text(a.next)
                self.title=normalize_content(self.title)
#                self.title=replace_with(self.title,'&gt;','>')
                return
        raise   
    
class bs(object):
    def __init__(self,path):
        html=open(path)
        injected=0
        data=html.read()
        html.close()
    #fromEncoding='gb18030'
        charset='gb18030'
        self.soup=BeautifulSoup(data,fromEncoding=charset)
        if self.soup.originalEncoding != charset:
            encoding_bug = charset
            